//
//  V4Home01View.m
//  XinKaiFa55like
//
//  Created by 55like on 22/09/2017.
//  Copyright © 2017 55like lj. All rights reserved.
//

#import "V4Home02View.h"
#import "RHTableView.h"
#import "V4LicensePlateViewController.h"
#import "V4TestLicensePlateContentViewController.h"

@interface V4Home02View ()<UITableViewDelegate,UITableViewDataSource>
{
    
    UIScrollView *scrollTypeBG;
    UIView *viewHeaderBG;
    UIPageControl *_pageControl;
    UIView*_titleView;
    
}
@property(nonatomic,strong)RHTableView*mtableView;

@end

@implementation V4Home02View
#pragma mark  开始
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        [self addView];
//        [self request];
    }
    
    return self;
}
#pragma mark -  写UI
-(void)addView{
    _mtableView =[[RHTableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    
    _mtableView.delegate = self;
    _mtableView.dataSource = self;
    _mtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_mtableView showRefresh:YES LoadMore:YES];
    [self addSubview:_mtableView];
    
    UIView*viewheader=[RHMethods viewWithFrame:CGRectMake(0, 0, kScreenWidth, 240) backgroundcolor:zrgbwhiteColor superView:nil];
    [_mtableView setTableHeaderView:viewheader];
    viewHeaderBG=viewheader;
    UIView*titleView=[[UIView alloc]initWithFrame:CGRectMake(0, 200, kScreenWidth, 40)];
    [viewheader addSubview:titleView];
    _titleView=titleView;
    
   [RHMethods imageviewWithFrame:CGRectMake(10, 13, 15, 14) defaultimage:@"mz" supView:titleView];
    [RHMethods lableX:30 Y:13 W:0 Height:14 font:14 superview:titleView withColor:rgb(220,169,32) text:@"牌照推荐"];
    [RHMethods viewWithFrame:CGRectMake(0, 39.5, kScreenWidth, 0.5) backgroundcolor:rgbLineColor superView:titleView];
    
    
    //    _mtableView.dataArray=[NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@"",@""]];
    //    [_mtableView reloadData];
    
    
    
}



-(void)loadHeaderTypeBg{
    if (!scrollTypeBG) {
        scrollTypeBG=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
        scrollTypeBG.pagingEnabled=YES;
        scrollTypeBG.delegate=self;
        [viewHeaderBG addSubview:scrollTypeBG];
        
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, YH(scrollTypeBG)-20, kScreenWidth, 20)];
        _pageControl.userInteractionEnabled = NO;
        _pageControl.currentPageIndicatorTintColor = rgbpublicColor;
        _pageControl.pageIndicatorTintColor = RGBACOLOR(200, 200, 200, 0.8);
        [viewHeaderBG addSubview:_pageControl];
    }else{
        for (UIView *view in scrollTypeBG.subviews) {
            [view removeFromSuperview];
        }
    }
//    NSArray *array=[[[Utility Share] userTypeData] objectForJSONKey:@"cate"];
    NSArray *array=[self.cataDic objectForJSONKey:@"list"];
    
    float fw=(kScreenWidth-20)/4;
    float fx=10;
    float fy=10;
    for (int i=0; i<[array count]; i++) {
        if (i%4==0 && i!=0) {
            fx=10;
            fy=100;
            if (i%8==0) {
                fx=kScreenWidth*(i/8)+10;//第二页
                fy=10;
            }
        }
        DLog(@"__x:%f________y:%f",fx,fy)
        [scrollTypeBG addSubview:[self tempTypeViewWithFrame:CGRectMake(fx, fy, fw, 90) withData:[array objectAtIndex:i] withIndex:i]];
        fx+=fw;
    }
    
    _pageControl.hidden=NO;
    scrollTypeBG.frameHeight=200;
    viewHeaderBG.frameHeight=240;
    if (array.count<9) {
        
        _pageControl.hidden=YES;
        scrollTypeBG.frameHeight=200-20;
        viewHeaderBG.frameHeight=240-20;
    }
    if (array.count<5) {
        
        scrollTypeBG.frameHeight=200-100;
        viewHeaderBG.frameHeight=240-100;
    }
    
    {
        _mtableView.noDataView.centerY=(_mtableView.frameHeight-viewHeaderBG.frameHeight)/2+viewHeaderBG.frameHeight;
        _mtableView.noDataView.centerX=kScreenWidth/2;
    }
    _titleView.frameBY=0;
     [_mtableView setTableHeaderView:viewHeaderBG];
    NSInteger page=ceil([array count]/8.0);
    [scrollTypeBG setContentSize:CGSizeMake(page*W(scrollTypeBG), H(scrollTypeBG))];
    _pageControl.numberOfPages = page;
    _pageControl.currentPage = 0;
    
    
}
-(UIView *)tempTypeViewWithFrame:(CGRect)_rect withData:(NSDictionary *)dic withIndex:(NSInteger )_index{
    UIView *view=[[UIView alloc] initWithFrame:_rect];
    view.tag=_index;
    UIImageView *imageV=[RHMethods imageviewWithFrame:CGRectMake((_rect.size.width-50)/2, 10, 50, 50) defaultimage:@"loadImageBG" contentMode:UIViewContentModeScaleAspectFit];
    [[Utility Share] viewLayerRound:imageV borderWidth:0.1 borderColor:[UIColor clearColor]];
    [view addSubview:imageV];
    [imageV imageWithURL:[dic valueForJSONStrKey:@"path"] useProgress:NO useActivity:NO];
    
    UILabel *lblTitl=[RHMethods labelWithFrame:CGRectMake(0, YH(imageV)+5, _rect.size.width, 20) font:fontSmallTitle color:rgbTxtDeepGray text:[dic valueForJSONStrKey:@"name"] textAlignment:NSTextAlignmentCenter];
    lblTitl.adjustsFontSizeToFitWidth=YES;
    [view addSubview:lblTitl];
    
    view.userInteractionEnabled=YES;
    [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(typeButtonClicked:)]];
    
    
    return view;
}
-(void)typeButtonClicked:(UITapGestureRecognizer *)tap{
    NSInteger index=tap.view.tag;
    DLog(@"index:%ld",(long)index)
    NSDictionary *dic=[[self.cataDic objectForJSONKey:@"list"] objectAtIndex:index];
    [UTILITY.currentViewController pushController:[V4LicensePlateViewController class] withInfo:@"type" withTitle:[dic valueForJSONStrKey:@"name"] withOther:dic];
}
#pragma mark  tableview网络请求
-(void)request{
    krequestParam
    //    app/products/mycollection   uid=470&token=e5f1a35a3621eac0d123dafa7a212823&cid=&keys=&page=1&pagesize=20&levelid=
    [dictparam setObject:@"%@" forKey:@"page"];
    [dictparam setObject:@"20" forKey:@"pagesize"];
    [dictparam setObject:[self.cataDic objectForJSONKey:@"id"] forKey:@"cid"];
    _mtableView.urlString=[NSString stringWithFormat:@"products/lists%@",dictparam.wgetParamStr];
    [_mtableView refresh];
    
    
}
#pragma mark - 按钮事件监听


#pragma mark - 代理事件



#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(RHTableView *)tableView{
    
    
    return 1;
    
}

-(NSInteger)tableView:(RHTableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return tableView.dataArray.count;
}

-(CGFloat)tableView:(RHTableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 94+4.5;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    return 0.001;
}

-(UIView *)tableView:(RHTableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
    
    
}

#pragma mark  cell处理
- (UITableViewCell *)tableView:(RHTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView ==_mtableView){
        //
        UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            cell.frameWidth=kScreenWidth;
            cell.backgroundColor=zrgbwhiteColor;
            UILabel*lbtitle=[RHMethods lableX:10 Y:16 W:kScreenWidth-20 Height:14 font:14 superview:cell withColor:nil text:@"上海梧桐融资租赁有限公司（简称梧桐公司）"];
            UILabel*lbleft1=[RHMethods lableX:25 Y:lbtitle.frameYH+ 15 W:kScreenWidth/2-25-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"售价：30万  "];
            UILabel*lbleft2=[RHMethods lableX:25 Y:lbleft1.frameYH+10   W:kScreenWidth/2-25-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"成立时间：2017-1-1"];
            UILabel*lbright1=[RHMethods lableX:kScreenWidth/2 Y:lbleft1.frameY W:kScreenWidth/2-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"注册资本：300万"];
            UILabel*lbright2=[RHMethods lableX:kScreenWidth/2 Y:lbleft2.frameY W:kScreenWidth/2-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"发布时间：2017-6-1"];
            UIView*viewlin=[RHMethods viewWithFrame:CGRectMake(0, 93.5, kScreenWidth, 5) backgroundcolor:rgbGray superView:cell];
            tableView.backgroundColor=rgbGray;
            
            
           UIImageView* imageRedV=[RHMethods imageviewWithFrame:CGRectMake(0, 7+2, 10, 10) defaultimage:@"redTip070116" contentMode:UIViewContentModeCenter];
            [cell addSubview:imageRedV];
            
            [cell setAddValue:lbtitle forKey:@"lbtitle"];
            [cell setAddValue:lbleft1 forKey:@"lbleft1"];
            [cell setAddValue:lbleft2 forKey:@"lbleft2"];
            [cell setAddValue:lbright1 forKey:@"lbright1"];
            [cell setAddValue:lbright2 forKey:@"lbright2"];
            [cell setAddValue:viewlin forKey:@"viewlin"];
            [cell setAddValue:imageRedV forKey:@"imageRedV"];
            
            
        }
        UILabel*lbtitle=[cell getAddValueForKey:@"lbtitle"];
        UILabel*lbleft1=[cell getAddValueForKey:@"lbleft1"];
        UILabel*lbleft2=[cell getAddValueForKey:@"lbleft2"];
        UILabel*lbright1=[cell getAddValueForKey:@"lbright1"];
        UILabel*lbright2=[cell getAddValueForKey:@"lbright2"];
        UILabel*imageRedV=[cell getAddValueForKey:@"imageRedV"];
        
        NSDictionary *dic=tableView.dataArray[indexPath.row];
        lbtitle.text=[dic objectForJSONKey:@"title"];
        imageRedV.frameX=lbtitle.frameX+[lbtitle.text widthWithFont:14];
         imageRedV.hidden=[[dic valueForJSONStrKey:@"isread"] isEqualToString:@"1"];
        lbleft1.text=[NSString stringWithFormat:@"售价：%@",[dic objectForJSONKey:@"price"]];
        lbright1.text=[NSString stringWithFormat:@"注册资本：%@",[dic objectForJSONKey:@"regcapital"]];
        lbleft2.text=[NSString stringWithFormat:@"成立时间：%@",[dic objectForJSONKey:@"regtime"]];
        lbright2.text=[NSString stringWithFormat:@"发布时间：%@",[dic objectForJSONKey:@"ctime"]];
        
        return cell;
        
    }
    return [UITableViewCell new];
}
#pragma mark  选中cell
-(void)tableView:(RHTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic=tableView.dataArray[indexPath.row];
    
    [UTILITY.currentViewController pushController:[V4TestLicensePlateContentViewController class] withInfo:@"Insurance" withTitle:@"牌照详情" withOther:dic];
    if ([[dic valueForJSONStrKey:@"isread"] isEqualToString:@"0"]) {
        [dic setValue:@"1" forKey:@"isread"];
        [tableView.dataArray replaceObjectAtIndex:indexPath.row withObject:dic];
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
    }
}
@end
